class Time {
  constructor(private time: number, private showTime: boolean) { }

  /**
   * 获取今天的时间戳
   * @return 今天时间的时间戳
   */
  private getTodayTime(): number {
    const date: Date = new Date();
    return date.getTime()
  }

  /**
   * 获取时分
   */
  private getHoursMin(date: Date): string {
    const hours: string = date.getHours() < 10 ? `0${date.getHours()}` : `${date.getHours()}`;
    const minutes: string = date.getMinutes() < 10 ? `0${date.getMinutes()}` : `${date.getMinutes()}`;

    return `${hours}:${minutes}`
  }

  /**
   * 获取该天的星期
   * @param time - 该天的时间戳
   */
  private getWeek(time: number): string {
    let txt = '';
    const week: number = new Date(time).getDay();
    switch (week) {
      case 0: txt = '星期天'; break;
      case 1: txt = '星期一'; break;
      case 2: txt = '星期二'; break;
      case 3: txt = '星期三'; break;
      case 4: txt = '星期四'; break;
      case 5: txt = '星期五'; break;
      case 6: txt = '星期六'; break;
    }

    return txt
  }

  /**
   * 格式化日期
   * @return 格式化后的日期(如2021/10/10)
   */
  private getFormatDate(): string {
    const date = new Date(this.time);
    const year = date.getFullYear();
    const month: string = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : `${(date.getMonth() + 1)}`;
    const day: string = date.getDate() < 10 ? '0' + date.getDate() : `${date.getDate()}`;

    return `${year}/${month}/${day}${this.showTime ? ` ${this.getHoursMin(date)}` : ''}`;
  }

  /**
   * 将时间戳转换为具体的时间
   * @return 具体的时间
   */
  getFormatTime(): string {
    const { floor } = Math;
    let txt: string = '';
    /**
     * 获取现在的时间戳
     */
    const now: number = this.getTodayTime();
    /**
     * 将用户收到的最新消息的时间和现在的时间的差转换为秒
     */
    const timestamp: number = (now - this.time) / 1000;
    const beforeDate: Date = new Date(this.time);
    if (floor(timestamp / 86400) <= 0) {
      if (beforeDate.getDay() === new Date(now).getDay())
        txt = `${this.showTime ? `今天 ${this.getHoursMin(beforeDate)}` : this.getHoursMin(beforeDate)}`;
      else txt = `昨天${this.showTime ? ` ${this.getHoursMin(beforeDate)}` : ''}`
    } else if (floor(timestamp / 86400) < 2)
      txt = `昨天${this.showTime ? ` ${this.getHoursMin(beforeDate)}` : ''}`
    else if (floor(timestamp / 86400) < 7) {
      // 获取今天的星期
      const NowWeek: number = new Date().getDay();
      switch (NowWeek) {
        case 0: txt = this.getWeek(this.time) +
          `${this.showTime ? ` ${this.getHoursMin(beforeDate)}` : ''}`;
          break;
        case 1: case 2: txt = this.getFormatDate(); break;
        case 3: {
          if (floor(timestamp / 86400) === 2) {
            txt = `星期一${this.showTime ? ` ${this.getHoursMin(beforeDate)}` : ''}`;
            break;
          }
          else { txt = this.getFormatDate(); break; }
        }
        case 4: {
          if (floor(timestamp / 86400) <= 3) {
            txt = this.getWeek(this.time) +
              `${this.showTime ? ` ${this.getHoursMin(beforeDate)}` : ''}`;
            break;
          }
          else { txt = this.getFormatDate(); break; }
        }
        case 5: {
          if (floor(timestamp / 86400) <= 4) {
            txt = this.getWeek(this.time) +
              `${this.showTime ? ` ${this.getHoursMin(beforeDate)}` : ''}`;
            break;
          }
          else { txt = this.getFormatDate(); break; }
        }
        case 6: {
          if (floor(timestamp / 86400) <= 5) {
            txt = this.getWeek(this.time) +
              `${this.showTime ? ` ${this.getHoursMin(beforeDate)}` : ''}`;
            break;
          }
          else { txt = this.getFormatDate(); break; }
        }
      }
    } else txt = this.getFormatDate()

    return txt;
  }
}

export default Time